import { Injectable } from '@angular/core'
import {
  IRiskControlQueryParams,
  RiskControlScheme,
  RiskControlRule,
  IRiskControlBiz,
} from '@shared/interfaces/risk-control'
import { Observable, throwError } from 'rxjs'
import { COMB_URL, COMB_URL2 } from 'app/utils/contraints'
import { HttpClient } from '@angular/common/http'
import { preProcessParams, encodeParams } from '@shared/decorators'
import { catchError, map, tap } from 'rxjs/operators'
import { IGenericPagination } from '@shared/interfaces/pagination'

@Injectable()
export class RiskAlinterService {
  constructor(private http: HttpClient) {}

  @preProcessParams()
  @encodeParams()
  getList(queryParams: IRiskControlQueryParams, str?: string): Observable<IGenericPagination<RiskControlScheme>> {
    const url = `${COMB_URL}/camel/risk/schemes?${str}`
    return this.http.get<IGenericPagination<RiskControlScheme>>(url)
  }

  create(body: RiskControlScheme): Observable<boolean> {
    const url = `${COMB_URL}/camel/risk/scheme`
    return this.http.post<boolean>(url, body)
  }

  getOne(schemeCode: string): Observable<RiskControlScheme> {
    const url = `${COMB_URL}/camel/risk/scheme/${schemeCode}`
    return this.http.get<RiskControlScheme>(url, { withCredentials: true })
  }

  update(schemeCode: string, body: RiskControlScheme): Observable<RiskControlScheme> {
    const url = `${COMB_URL}/camel/risk/scheme/${schemeCode}`
    return this.http.put<RiskControlScheme>(url, body)
  }

  delete(schemeCode: string): Observable<boolean> {
    const url = `${COMB_URL}/camel/risk/scheme/${schemeCode}`
    return this.http.delete<boolean>(url)
  }

  getRuleOne(type: string, ruleId: number): Observable<RiskControlRule> {
    const url = `${COMB_URL}/camel/risk/rule/${type}/${ruleId}`
    return this.http.get<RiskControlRule>(url)
  }

  deleteRule(type: string, ruleId: number): Observable<boolean> {
    const url = `${COMB_URL}/camel/risk/rule/${type}/${ruleId}`
    return this.http.delete<boolean>(url)
  }

  updateRule(type: string, ruleId: number, body: RiskControlRule): Observable<RiskControlRule> {
    const url = `${COMB_URL}/camel/risk/rule/${type}/${ruleId}`
    return this.http.put<RiskControlRule>(url, body)
  }

  getBizs(schemeCode: string): Observable<IRiskControlBiz[]> {
    const url = `${COMB_URL}/camel/risk/scheme/${schemeCode}/biz`
    return this.http.get<IRiskControlBiz[]>(url)
  }

  createRule(schemeCode: string, body: RiskControlRule): Observable<boolean> {
    const url = `${COMB_URL}/camel/risk/scheme/${schemeCode}/rule`
    return this.http
      .post<boolean>(url, body, {
        observe: 'response',
      })
      .pipe(
        map(resp => {
          const { status } = resp
          return true
        }),
      )
  }

  /** 冻结 */
  freeze(schemeCode: string): Observable<boolean> {
    const url = `${COMB_URL}/camel/risk/scheme/${schemeCode}/freeze`
    return this.http.put<boolean>(url, {})
  }

  /** 解冻 */
  unfreeze(schemeCode: string): Observable<boolean> {
    const url = `${COMB_URL}/camel/risk/scheme/${schemeCode}/unfreeze`
    return this.http.put<boolean>(url, {})
  }
}
